/*
 * Created on Jul 21, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2005
 *
 * Contains information for a term.
 */
public class TermContainer extends Container {

    // The map of timer buckets for all databases
    protected static Hashtable dbStores = new Hashtable();
    // the auto assigned id
    private IntegerItem termAssignedId =
    	new IntegerItem(SchoolDBSQL.TERMASSIGNEDID, "", false);
    // The name of the term 
    private StringItem termName =
        new StringItem(SchoolDBSQL.TERMNAME, "", false);
    // The start date of the term
    private DateNoTimeItem termStartDay =
        new DateNoTimeItem(SchoolDBSQL.TERMSTART, "", false);
    // The end date of the term
    private DateNoTimeItem termEndDay =
        new DateNoTimeItem(SchoolDBSQL.TERMEND, "", false);
    // A list of the hours the term occurs
    private ArrayList hours = new ArrayList(5);

    // Default constructor
    private TermContainer() {

    }

    /**
     * Constructor creates a term container with values loaded from the database
     * using the name passed in as a parameter.
     * @param newName The name of the term to load.
     * @param dbName The name of the school database
     */
    private TermContainer(String assignedId, String dbName) {
        this.getTermAssignedId().setValue(assignedId);
        SchoolDBSQL.getSchoolDBSQL(dbName).loadTerm(this.getValidationItems(), this.getTermAssignedId());
    }

    public static TermContainer getTermContainer(String assignedId, String dbName) {
    	TermContainer returnVal = null;
        DBStore dbs = null;

        if ("".equals(assignedId)) {
            returnVal = new TermContainer();
        }
        else {
            if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
                dbs = new DBStore();
                dbStores.put(dbName, dbs);
                dbs.start();
            }
            if ((returnVal = (TermContainer) dbs.getContainer(assignedId)) == null) {
                returnVal = new TermContainer();
                returnVal.getTermAssignedId().setValue(assignedId);
                SchoolDBSQL.getSchoolDBSQL(dbName).loadTerm(returnVal.getValidationItems(), returnVal.getTermAssignedId());
                dbs.addContainer(returnVal, assignedId);
            }
        }
        return returnVal;
    }

    /* (non-Javadoc)
     * @see schoolsource.model.Container#getValidationItems()
     */
    public Item[] getValidationItems() {
        ArrayList al = new ArrayList();
        al.add(termAssignedId);
        al.add(termName);
        al.add(termStartDay);
        al.add(termEndDay);

        return (Item[]) al.toArray(new Item[al.size()]);
    }

    /**
     * Getter for the name of the term.
     * @return The name of the term.
     */
    public StringItem getTermName() {
        return termName;
    }

    /**
     * Getter for the name of the term.
     * @return The name of the term.
     */
    public DateNoTimeItem getTermStartDay() {
        return termStartDay;
    }

    /**
     * Getter for the name of the term.
     * @return The name of the term.
     */
    public DateNoTimeItem getTermEndDay() {
        return termEndDay;
    }

    /**
     * Prints the values of the container.
     */
    public void printItems() {
        SchoolLogger.logger.log(
            Level.INFO,
            "************************************************");
        Item iList[] = getValidationItems();
        for (int j = 0; j < iList.length; j++) {
            if (iList[j] != null) {
                SchoolLogger.logger.log(
                    Level.INFO,
                    iList[j].getFieldName() + ": " + iList[j].toString());
            }
        }
    }

	public IntegerItem getTermAssignedId() {
		return termAssignedId;
	}
}
